package com.feifan.monthhelper.calculate;

import com.blankj.utilcode.util.LogUtils;
import com.feifan.monthhelper.bean.db.DayRecordBean;
import com.feifan.monthhelper.constant.Constant;

import java.util.Date;

/**
 * 安全期算法
 */
public class SafeDayCalculate extends AbstractMenstrualCalculate<DayRecordBean> {

    @Override
    boolean canCalculate(DayRecordBean request) {
        long requestDay = request.createDate.getTime()/(1000 * 60 * 60 * 24);

        long menstrualDay = (getDate().getTime()/(1000 * 60 * 60 * 24));

        if(requestDay >= menstrualDay) {
            return false;
        }
        //传进来的要计算的日期，过滤掉月经期和排卵期之后倒是安全期
        return true;

//        System.out.println("requestDay = " + requestDay + " menstrualDay  = " + menstrualDay);
//        System.out.println("SafeDayCalculate getOffset(requestDay, menstrualDay) <= 7 " + (getOffset(requestDay, menstrualDay) <= 7));
//        System.out.println("SafeDayCalculate getOffset(requestDay, menstrualDay) < getMenstrual_period() + 8 " + (getOffset(requestDay, menstrualDay) < getMenstrual_period() + 8));
//        if(requestDay > menstrualDay) {
//            System.out.println("SafeDayCalculate getOffset(requestDay, menstrualDay) = 7 " + (getOffset(requestDay, menstrualDay)));
//            //月经结束时间后8天,或者下一个月月周期开始前7天
//            return (getOffset(requestDay, menstrualDay) <= getMenstrual_period() + 8) || (Math.abs(getOffset(requestDay, menstrualDay) - getSpan_period()) <= 7);
//        } else if(requestDay < menstrualDay) {
//            System.out.println("SafeDayCalculate getOffset(requestDay, menstrualDay) = " + (getOffset(requestDay, menstrualDay) ));
//            //月经前7天安全期
//            return getOffset(requestDay, menstrualDay) <= 7;
//        }
//        return false;
    }

    //取计算日期和经期开始日期对两个周期间隔取模
    private long getOffset(long requestDay, long menstrualDay) {
        System.out.println("SafeDayCalculate getSpan_period = " + getSpan_period() + "  getMenstrual_period() = " + getMenstrual_period());
        return Math.abs(requestDay - menstrualDay) % (getSpan_period());
    }

    @Override
    DayRecordBean calculate(DayRecordBean request) {
        request.state = Constant.MenstrualState.SAFE_DAY;
        return request;
    }
}
